package com.airspyar;

import android.util.Log;

public abstract class ThreadExtended extends Thread {
	private boolean killed=false;
	private boolean paused=true;
	private final Object killMutex=new Object();
	private final Object pauseMutex=new Object();
	
	protected abstract void loop();
	
	public void kill(){
		synchronized(killMutex){
			killed=true;
		}
	}
	
	protected boolean isKilled(){
		synchronized(killMutex){
			return killed;
		}
	}
	
	public void setPaused(boolean p){
		synchronized(pauseMutex){
			paused=p;
		}
	}
	
	public boolean isPaused(){
		synchronized(pauseMutex){
			return paused;
		}
	}
	
	@Override
	public void run(){
		while(!isKilled()){
			if(isPaused()){
				try{
					sleep(100);
				}catch(InterruptedException e){
					if(AirSpyActivity.LOG_ERROR)
						Log.e("ThreadExtended exception", e.getMessage());
				}
				continue;
			}
			
			loop();
			Thread.yield();
		}
	}
	
}
